﻿/*
 * Created by SharpDevelop.
 * User: matsu
 * Date: 6/5/2010
 * Time: 10:23 PM
 * 
 * Copyright (c) 2011 Matthew MacGregor
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Feedback
{
    /// <summary>
    /// The DebugConsole provides debugging feedback for an application, printing
    /// the information to a separate window.  
    /// 
    /// It is safe to call methods on the DebugConsole from other threads.
    /// </summary>
    public partial class DebugConsole : Form
    {
        // Allows thread-safe access to text property on a TextBox control.
        delegate void TextCallback(string text);


        public DebugConsole()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            

        }
        
        /// <summary>
        /// Allows for thread-safe updating of the DebugConsole.
        /// </summary>
        /// <param name="text"></param>
        public void SetText( string text )
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.textBox.InvokeRequired)
            {
                TextCallback d = new TextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBox.Text += text;
            }
        }
        
        /// <summary>
        /// Allows for thread-safe appending of text to the DebugConsole.
        /// </summary>
        /// <param name="text"></param>
        public void AppendText( string text )
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.textBox.InvokeRequired)
            {
                TextCallback d = new TextCallback(AppendText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBox.Text += text;
            }

        }
        
        /// <summary>
        /// The PrintLine method is the preferred way to print text to the debug window. A
        /// line ending will be supplied automatically.
        /// 
        /// This method is thread-safe.
        /// </summary>
        /// <param name="text"></param>
        public void PrintLine( string text )
        {
            AppendText( text + Environment.NewLine );
        }
        
        /// <summary>
        /// Similar to PrintLine, only a line ending is not automatically appended.
        /// 
        /// This method is thread-safe.
        /// </summary>
        /// <param name="text"></param>
        public void Print( string text )
        {
            AppendText( text );
        }
        
        /// <summary>
        /// Clears the debug console of all text.
        /// 
        /// This method is thread-safe.
        /// </summary>
        public void ClearText( )
        {
           SetText("");
        }
        
        void DebugConsoleFormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason != CloseReason.FormOwnerClosing)
            {
                //Hides the form instead of closing it.  You will have to close
                //it manually!
                this.Hide();
                e.Cancel = true;
            }
        }
    }
}
